package @@outputPackage@@;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.json.client.*;

public class @@shortClassName@@Jsonizer {
    public static @@className@@ toJava(JSONValue arg0, JsonizerHelper helper) {
        if (arg0.isNull() != null) {
            return null;
        }

        String ref = arg0.isString().stringValue();

        @@className@@ toReturn = (@@className@@) helper.getObject(ref);

        if (toReturn == null) {
            JSONValue value = helper.getJson(ref);

            JSONObject jsonObject = value.isObject();
            if (jsonObject == null) {
                throw new JSONException("expected JSONObject");
            }
            // assume we received a valid object

            String jsonclass = ObjectJsonizer.toJavaString(jsonObject.get("__jsonclass__"));
            if ("@@className@@".equals(jsonclass) == false) {
                throw new JSONException("expected @@className@@, actual " + jsonclass);
            }
            // assume the object is of the proper class type

            toReturn = new @@className@@();
            helper.putObject(ref, toReturn);

            @@beanSetters@@
        }

        return toReturn;
    }

    public static JSONValue toJson(@@className@@ arg0, JsonizerHelper helper) {
        if (arg0 == null) {
            return JSONNull.getInstance();
        }

        String ref = helper.fetchRef(arg0);

        if (ref == null) {
            ref = helper.createRef(arg0);

            JSONObject json = new JSONObject();
            json.put("__jsonclass__", new JSONString("@@className@@"));

            @@beanGetters@@

            helper.putJson(ref, json);
        }

        return new JSONString(ref);
    }

    public static List<@@className@@> toJavaList(JSONValue arg0, JsonizerHelper helper) {
        if (arg0.isNull() != null) {
            return null;
        }

        JSONArray jsonArray = arg0.isArray();
        if (jsonArray == null) {
            throw new JSONException("expected JSONNull or JSONArray");
        }

        List<@@className@@> toReturn = new ArrayList<@@className@@>();
        for (int i = 0, n = jsonArray.size(); i < n; i++) {
            toReturn.add(toJava(jsonArray.get(i), helper));
        }

        return toReturn;
    }

    public static JSONValue toJsonList(List<@@className@@> arg0, JsonizerHelper helper) {
        if (arg0 == null) {
            return JSONNull.getInstance();
        }

        JSONArray toReturn = new JSONArray();
        for (int i = 0, n = arg0.size(); i < n; i++) {
            toReturn.set(i, toJson(arg0.get(i), helper));
        }

        return toReturn;
    }

    public static @@className@@[] toJavaArray(JSONValue arg0, JsonizerHelper helper) {
        if (arg0.isNull() != null) {
            return null;
        }

        JSONArray jsonArray = arg0.isArray();
        if (jsonArray == null) {
            throw new JSONException("expected JSONNull or JSONArray");
        }

        @@className@@[] toReturn = new @@className@@[jsonArray.size()];
        for (int i = 0, n = jsonArray.size(); i < n; i++) {
            toReturn[i] = toJava(jsonArray.get(i), helper);
        }

        return toReturn;
    }

    public static JSONValue toJsonArray(@@className@@[] arg0, JsonizerHelper helper) {
        if (arg0 == null) {
            return JSONNull.getInstance();
        }

        JSONArray toReturn = new JSONArray();
        for (int i = 0, n = arg0.length; i < n; i++) {
            toReturn.set(i, toJson(arg0[i], helper));
        }

        return toReturn;
    }

    public static Map<String, @@className@@> toJavaMap(JSONValue arg0, JsonizerHelper helper) {
        if (arg0.isNull() != null) {
            return null;
        }

        JSONObject jsonObject = arg0.isObject();
        if (jsonObject == null) {
            throw new JSONException("expected JSONNull or JSONObject");
        }

        Map<String, @@className@@> toReturn = new HashMap<String, @@className@@>();
        for (String key : jsonObject.keySet()) {
            @@className@@ value = toJava(jsonObject.get(key), helper);

            toReturn.put(key, value);
        }

        return toReturn;
    }

    public static JSONValue toJsonMap(Map<String, @@className@@> arg0, JsonizerHelper helper) {
        if (arg0 == null) {
            return JSONNull.getInstance();
        }

        JSONObject toReturn = new JSONObject();
        for (String key : arg0.keySet()) {
            JSONValue value = toJson(arg0.get(key), helper);

            toReturn.put(key, value);
        }

        return toReturn;
    }
}
